package cn.xx.user.service.impl;

import cn.xx.basic.constant.PetHomeConstant;
import cn.xx.basic.constant.VerifactionCodeConstant;
import cn.xx.basic.exception.CustomException;
import cn.xx.basic.service.impl.BaseServiceImpl;
import cn.xx.basic.util.MD5Utils;
import cn.xx.basic.util.StrUtils;
import cn.xx.user.domain.LoginInfo;
import cn.xx.user.domain.User;
import cn.xx.user.dto.UserDto;
import cn.xx.user.mapper.LoginInfoMapper;
import cn.xx.user.mapper.UserMapper;
import cn.xx.user.service.IUserService;
import cn.xx.user.service.IUserService2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl2 extends BaseServiceImpl<User> implements IUserService2 {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void checkPhone(String phone) throws CustomException {
        // 获取对象
        User user = userMapper.checkPhone(phone);
        // 如果存在代表手机号重复
        if (user != null) {
            // 抛出异常信息
            throw new CustomException("手机已经存在！");
        }
    }






    /**
     * 校验前端传递的数据
     * 检查注册的信息
     * @param userDto
     * @throws CustomException
     */
    @Override
    public void register(UserDto dto) throws CustomException {
        checkData(dto);
        User user = creatUser(dto);
        LoginInfo loginInfo = createLoginInfo(user);
        loginInfoMapper.save(loginInfo);
        userMapper.save(user);
        user.setLoginInfo(loginInfo);
        userMapper.update(user);
    }

    private LoginInfo createLoginInfo(User user) {
        LoginInfo loginInfo = new LoginInfo();
        BeanUtils.copyProperties(user, loginInfo);
        loginInfo.setType(PetHomeConstant.HOME);
        return loginInfo;

    }


    private User creatUser(UserDto dto) {
        User user = new User();
        user.setPhone(dto.getPhone());
        user.setSalt(StrUtils.getComplexRandomString(10));
        user.setPassword(MD5Utils.encrypByMd5(dto.getPassword()+user.getSalt()));
        return user;
    }

    private void checkData(UserDto dto) throws CustomException {
        if (StringUtils.isEmpty(dto.getPhone() == null)) {
            throw new CustomException("手机号码为空");
        }
        LoginInfo loginInfo = loginInfoMapper.checkPhoneAndType(dto.getPhone(), PetHomeConstant.HOME);
        if(loginInfo != null) {
            throw new CustomException("手机号码已被注册");
        }
        if (StringUtils.isEmpty(dto.getCode() == null)) {
            throw new CustomException("验证码为空");
        }
        String codeValue = (String) redisTemplate.opsForValue().get(dto.getPhone() + ":" + VerifactionCodeConstant.REG_CODE);
        String code = codeValue.split(":")[0];
        if (code== null) {
            throw new CustomException("验证码已经失效，请重新发送");
        }
        if (!dto.getCode().equals(code)) {
            throw new CustomException("验证码错误");
        }
        if (StringUtils.isEmpty(dto.getPassword() == null)) {
            throw new CustomException("密码为空");
        }
        if (StringUtils.isEmpty(dto.getConfigPassword() == null)) {
            throw new CustomException("确认密码为空");
        }
        if (StringUtils.isEmpty(!dto.getPassword().equals(dto.getConfigPassword()))) {
            throw new CustomException("两次密码不一致");
        }
    }


}
